//
//  TXcmd.swift
//  MobileRTCSwiftSample
//
//  Created by zhangheng on 2019/10/11.
//  Copyright © 2019 Zoom Video Communications, Inc. All rights reserved.
//

import Foundation
import TXLiteAVSDK_TRTC_Mac

///// Window
/*
 //用接口直接发的ID，目前就这三个，所有主持人相关的接口都按3发送
public const uint RaiseHand      = 1;//举手
public const uint DownHand       = 2;//放下举手
public const uint HostAction     = 3;//主持人消息

 //所有主持人消息，都会发送以下对象序列化后的json数据，注意大小写可能不是全小写，现在没代码记不得了，可以输出出来看一下
 public class HostEntity{
 public uint actionid //id为以下列出的所有主持人操作ID

 public string userid{get;set}//用户ID，不需要是可不填
 }

 //以下为全部主持人操作Id

public const uint MuteAll        = 104;//全部静音
public const uint UnMuteAll      = 105;//全部取消静音
public const uint StopShare      = 106;//禁止共享
public const uint AllowShare     = 107;//允许共享
public const uint DownAllHand    = 108;//放下所有举手
public const uint DownUserHand   = 109;//放下某个用户的举手
public const uint CloseMeeting   = 102;//结束会议
public const uint RemoveUser     = 110;//移出某个用户
public const uint MuteUser       = 111;//静音某个用户
public const uint UnMuteUser     = 112;//取消某个用户的静音
public const uint FocuseUser     = 113;//将某个用户设为焦点
public const uint MeetingTimeout = 114;//会议超时
 */



/////////////////     通用权利       /////////////////

let mt_cmd_raiseHand             = 1//举手
let mt_cmd_downHand              = 2//放下举手
let mt_cmd_hostAction            = 3//主持人消息
let mt_cmd_barrage               = 9//弹幕消息
let mt_cmd_status                = 50//用户状态

////////////////       主持人      //////////////////

let mt_cmd_host_muteAll          = 104//全体静音
let mt_cmd_host_unmuteAll        = 105//全体静音解除
let mt_cmd_host_stopShare        = 106//禁止共享
let mt_cmd_host_allowShare       = 107//允许共享
let mt_cmd_host_downAllHand      = 108//放下所有举手
let mt_cmd_host_downUserHand     = 109//放下某个用户的举手
let mt_cmd_host_removeUser       = 110//移出某个用户
let mt_cmd_host_muteUser         = 111//静音某个用户
let mt_cmd_host_unMuteUser       = 112//取消某个用户的静音
let mt_cmd_host_focuseUser       = 113//将某个用户设为焦点
let mt_cmd_host_meetingTimeout   = 114//会议超时
let mt_cmd_host_exit             = 102//结束会议

let mt_cmd_host_closeVideoAll    = 120//关闭全部视频
let mt_cmd_host_showVideoAll     = 121//开启全部视频
let mt_cmd_host_closeVideoUser   = 122//关闭用户视频
let mt_cmd_host_showVideoUser    = 123//开启用户视频

let mt_cmd_host_disDanmu         = 127//禁止聊天
let mt_cmd_host_danmu            = 128//开启聊天




class TXcmd: NSObject {
    
    static let shared = TXcmd()
    
    ///////////// 通用权利 //////////////
    
    //举手
    func handUp(userId:String=Td.sh().params.userId!,isok:Bool) -> () {
    
        let model = CmdMsgMode()
        model.userid = userId
        let mesg = model.toJSON()!.toJsonString()
        let data = mesg?.data(using: String.Encoding.utf8)
        let cmdCode = isok ? mt_cmd_raiseHand:mt_cmd_downHand
        TRTCCloud.sharedInstance()?.sendCustomCmdMsg(cmdCode, data: data, reliable: true, ordered: true)
    }
    
    
    //弹幕
    func barrage(userId:String=Td.sh().params.userId!,txt:String) {
        let model = CmdMsgMode()
        model.userid = userId
        model.txt = txt
        let mesg = model.toJSON()!.toJsonString()
        let data = mesg?.data(using: String.Encoding.utf8)
        //用命令退出全部设备
        TRTCCloud.sharedInstance()?.sendCustomCmdMsg(mt_cmd_barrage, data: data, reliable: true, ordered: true)
    }
    
    /////////////  主持人权利  ///////////
    
    //是否共享
    func allShare(isok:Bool) {
        let actionId = isok ? mt_cmd_host_allowShare:mt_cmd_host_stopShare
        let model = CmdMsgMode()
        model.actionid = actionId as NSNumber
        let mesg = model.toJSON()!.toJsonString()
        let data = mesg?.data(using: String.Encoding.utf8)
        TRTCCloud.sharedInstance()?.sendCustomCmdMsg(mt_cmd_hostAction, data: data, reliable: true, ordered: true)
    }
    
    //是否全体静音
    func allMute(isok:Bool) {
        let actionId = isok ? mt_cmd_host_muteAll:mt_cmd_host_unmuteAll
        let model = CmdMsgMode()
        model.actionid = actionId as NSNumber
        let mesg = model.toJSON()!.toJsonString()
        let data = mesg?.data(using: String.Encoding.utf8)
        TRTCCloud.sharedInstance()?.sendCustomCmdMsg(mt_cmd_hostAction, data: data, reliable: true, ordered: true)
    }
    
    
    //踢人
    func kick(userId:String)  {
        let model = CmdMsgMode()
        model.userid = userId
        model.actionid = mt_cmd_host_removeUser as NSNumber
        let mesg = model.toJSON()!.toJsonString()
        let data = mesg?.data(using: String.Encoding.utf8)
        
        TRTCCloud.sharedInstance().sendCustomCmdMsg(mt_cmd_hostAction, data: data, reliable: true, ordered: true)
    }
    
    //关闭房间
    func exit() {
        let model = CmdMsgMode()
        model.actionid =  mt_cmd_host_exit as NSNumber
        let mesg = model.toJSON()!.toJsonString()
        let data = mesg?.data(using: String.Encoding.utf8)
        //用命令退出全部设备
        TRTCCloud.sharedInstance().sendCustomCmdMsg(3, data: data!, reliable: true, ordered: true)
    }
    
    //主持人解除举手
    func downAllHand() {
        let model = CmdMsgMode()
        model.actionid = mt_cmd_host_downAllHand as NSNumber
        let mesg = model.toJSON()!.toJsonString()
        let data = mesg?.data(using: String.Encoding.utf8)
        TRTCCloud.sharedInstance()?.sendCustomCmdMsg(0x3, data: data, reliable: true, ordered: true)
    }
    
    //主持人解除某用户举手
    func downUserHand(userId:String) {
        let model = CmdMsgMode()
        model.actionid = mt_cmd_host_downUserHand as NSNumber
        model.userid = userId
        let mesg = model.toJSON()!.toJsonString()
        let data = mesg?.data(using: String.Encoding.utf8)
        TRTCCloud.sharedInstance().sendCustomCmdMsg(mt_cmd_hostAction, data: data, reliable: true, ordered: true)
    }
    
    
    //设定焦点
    func focusUser(userId:String) {
        let model = CmdMsgMode()
        model.userid = userId
        model.actionid = mt_cmd_host_focuseUser as NSNumber
        let mesg = model.toJSON()!.toJsonString()
        let data = mesg?.data(using: String.Encoding.utf8)
        TRTCCloud.sharedInstance().sendCustomCmdMsg(mt_cmd_hostAction, data: data, reliable: true, ordered: true)
    }
    
   
    
    //是否全体关闭视频
    func videoAll(isok:Bool) {
        let acId = isok ? mt_cmd_host_showVideoAll:mt_cmd_host_closeVideoAll
        let model = CmdMsgMode()
        model.actionid = acId as NSNumber
        let mesg = model.toJSON()!.toJsonString()
        let data = mesg?.data(using: String.Encoding.utf8)
        TRTCCloud.sharedInstance()?.sendCustomCmdMsg(mt_cmd_hostAction, data: data, reliable: true, ordered: true)
    }
    
    //是否用户开启关闭视频
    func videoUser(userId:String,isok:Bool) {
        let acId = isok ? mt_cmd_host_showVideoUser:mt_cmd_host_closeVideoUser
        let model = CmdMsgMode()
        model.userid = userId
        model.actionid = acId as NSNumber
        let mesg = model.toJSON()!.toJsonString()
        let data = mesg?.data(using: String.Encoding.utf8)
        TRTCCloud.sharedInstance()?.sendCustomCmdMsg(mt_cmd_hostAction, data: data, reliable: true, ordered: true)
    }
    
    //是否用户静音
    func muteUser(userId:String,isok:Bool) {
        let acId = isok ? mt_cmd_host_muteUser:mt_cmd_host_unMuteUser
        let model = CmdMsgMode()
        model.userid = userId
        model.actionid = acId as NSNumber
        let mesg = model.toJSON()!.toJsonString()
        let data = mesg?.data(using: String.Encoding.utf8)
        TRTCCloud.sharedInstance()?.sendCustomCmdMsg(mt_cmd_hostAction, data: data, reliable: true, ordered: true)
    }
    
    
    
    //主持人弹幕
    func disDanmu(isok:Bool) {
        let cmd = isok ? mt_cmd_host_disDanmu:mt_cmd_host_danmu
        let model = CmdMsgMode()
        model.actionid = cmd as NSNumber
        let mesg = model.toJSON()!.toJsonString()
        let data = mesg?.data(using: String.Encoding.utf8)
        //用命令退出全部设备
        TRTCCloud.sharedInstance()?.sendCustomCmdMsg(mt_cmd_hostAction, data: data, reliable: true, ordered: true)
    }
  
}
